package nige.portable.composant;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import nige.portable.commun.Utils;

public class TextArea {

	private String texte = "";
	private int xPos = 0;
	private int yPos = 0;
	private int length = 0;
	private Font font = null;
	private Graphics graphParent = null;
	private Graphics graph = null;
	private Image image = null;
	private int lastCode = 0;
	
	public TextArea(Graphics graphT,Font fontT,
			String texteT,int xPosT, int yPosT, int lengthT) {
		font = fontT;
		texte = texteT;
		xPos = xPosT;
		yPos = yPosT;
		length = lengthT;
		graphParent = graphT;
		image = Image.createImage(lengthT,font.getHeight()+3);
		graph = image.getGraphics();
		eventOn(0);
	}
	
	public void eventOn(int code) {
		graph.setColor(Utils.getColor(230,230,230));
		graph.fillRect(0,0,length,font.getHeight()+3);
		graph.setColor(0);
		graph.drawRect(0,0,length,font.getHeight()+3);
		if(texte.indexOf("[")>=0) {
			texte = texte.substring(0,texte.indexOf("["));
		}
		int lenBef = texte.length();
		texte += codeCorres(code);
		if(lenBef == texte.length()) {
			lastCode = code;
			texte += resultChoice(code);
		}
		else {
			lastCode = 0;
		}
		if((code == 42) && (texte.length()>0)) {
			texte = texte.substring(0,texte.length()-1);
		}
		graph.drawString(texte.toUpperCase(),1,1,0);
		graphParent.drawImage(image,xPos,yPos,0);
	}
	
	private String resultChoice(int code) {
		String res = "";
		switch(code) {
			case 50 :
				res = "[a-b-c]";
				break;
			case 51 :
				res = "[d-e-f]";
				break;
			case 52 :
				res = "[g-h-i]";
				break;
			case 53 :
				res = "[j-k-l]";
				break;
			case 54 :
				res = "[m-n-o]";
				break;
			case 55 :
				res = "[p-q-r-s]";
				break;
			case 56 :
				res = "[t-u-v]";
				break;
			case 57 :
				res = "[w-x-y-z]";
				break;
		}
		return res;
	}
	
	private String codeCorres(int code) {
		String textTmp = "";
		switch(lastCode) {
			case 50 :
				switch(code) {
					case 49 :
						textTmp = "a";
						break;
					case 50 :
						textTmp = "b";
						break;
					case 51 :
						textTmp = "c";
						break;
				}
				break;
			case 51 :
				switch(code) {
					case 49 :
						textTmp = "d";
						break;
					case 50 :
						textTmp = "e";
						break;
					case 51 :
						textTmp = "f";
						break;
				}
				break;
			case 52 :
				switch(code) {
					case 49 :
						textTmp = "g";
						break;
					case 50 :
						textTmp = "h";
						break;
					case 51 :
						textTmp = "i";
						break;
				}
				break;
			case 53 :
				switch(code) {
					case 49 :
						textTmp = "j";
						break;
					case 50 :
						textTmp = "k";
						break;
					case 51 :
						textTmp = "l";
						break;
				}
				break;
			case 54 :
				switch(code) {
					case 49 :
						textTmp = "m";
						break;
					case 50 :
						textTmp = "n";
						break;
					case 51 :
						textTmp = "o";
						break;
				}
				break;
			case 55 :
				switch(code) {
					case 49 :
						textTmp = "p";
						break;
					case 50 :
						textTmp = "q";
						break;
					case 51 :
						textTmp = "r";
						break;
					case 52 :
						textTmp = "s";
						break;
				}
				break;
			case 56 :
				switch(code) {
					case 49 :
						textTmp = "t";
						break;
					case 50 :
						textTmp = "u";
						break;
					case 51 :
						textTmp = "v";
						break;
				}
				break;
			case 57 :
				switch(code) {
					case 49 :
						textTmp = "w";
						break;
					case 50 :
						textTmp = "x";
						break;
					case 51 :
						textTmp = "y";
						break;
					case 52 :
						textTmp = "z";
						break;
				}
				break;
		}
		return textTmp;
	}
	
}
